﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BackEnd.Facade.Interfaces;
using BackEnd.Integration;

namespace BackEnd.Facade.Implementations
{
    public class FeedbackManager : IFeedbackManager
    {
        public List<Domain.CustomerFeedback> GetServiceReviews(int id)
        {
            using (var ctx = new TaxiDataDataContext())
            {
                ctx.ObjectTrackingEnabled = false;

                var result = (from r in ctx.CustomerFeedbacks
                              where r.TaxiServiceId == id &&
                              r.IsApproved == true
                              orderby r.CreateDate descending
                              select new Domain.CustomerFeedback() { 
                                CarRating = r.CarRating,
                                Comment = r.Comment,
                                CreateDate = r.CreateDate,
                                DriverRating = r.DriverRating,
                                Id = r.CustomerFeedbackId,
                                IsApproved = r.IsApproved,
                                PriceRating = r.PriceRating,
                                ServiceRating = r.ServiceRating,
                                UserId = r.UserId,
                                FullName = r.FullName
                              }).ToList();
                return result;
            }
        }

        /// <summary>
        /// Returns last approved reviews with conjuction of
        /// taxi service object including names and id
        /// </summary>
        /// <param name="count">How many rows should be retrieved</param>
        /// <returns></returns>
        public List<Domain.CustomerFeedback> GetLastReviews(int count)
        {
            using (var ctx = new TaxiDataDataContext())
            {
                ctx.ObjectTrackingEnabled = false;

                var result = (from r in ctx.CustomerFeedbacks
                              where r.IsApproved == true
                              orderby r.CreateDate descending
                              select new Domain.CustomerFeedback()
                              {
                                  CarRating = r.CarRating,
                                  Comment = r.Comment,
                                  CreateDate = r.CreateDate,
                                  DriverRating = r.DriverRating,
                                  Id = r.CustomerFeedbackId,
                                  IsApproved = r.IsApproved,
                                  PriceRating = r.PriceRating,
                                  ServiceRating = r.ServiceRating,
                                  UserId = r.UserId,
                                  FullName = r.FullName,
                                  Service = new Domain.TaxiService()
                                  {
                                      Id = r.TaxiServiceId,
                                      NameAM = r.TaxiService.NameAM,
                                      NameEN = r.TaxiService.NameEN,
                                      NameRU = r.TaxiService.NameRU,
                                  }
                              }).Take(count).ToList();
                return result;
            }
        }

        public void AddNewReview(Domain.CustomerFeedback review, int serviceId)
        {
            using (var ctx = new TaxiDataDataContext())
            {
                var entity = new CustomerFeedback()
                {
                    CarRating = review.CarRating,
                    Comment = review.Comment,
                    CreateDate = review.CreateDate,
                    DriverRating = review.DriverRating,
                    CustomerFeedbackId = review.Id,
                    IsApproved = review.IsApproved,
                    PriceRating = review.PriceRating,
                    ServiceRating = review.ServiceRating,
                    UserId = review.UserId,
                    TaxiServiceId = serviceId
                };

                ctx.CustomerFeedbacks.InsertOnSubmit(entity);
                ctx.SubmitChanges();
       
            }
        }

        public bool DeleteReview(long id)
        {
            using (var ctx = new TaxiDataDataContext())
            {
                CustomerFeedback entity;
                try
                {

                    entity = ctx.CustomerFeedbacks.Single(c => c.CustomerFeedbackId == id && !c.IsApproved);
                }
                catch (Exception ex)
                {
                    return false;
                }
                ctx.CustomerFeedbacks.DeleteOnSubmit(entity);
                ctx.SubmitChanges();
                return true;
            }
            
        }

        public List<Domain.CustomerFeedback> GetUnapprovedReviews()
        {
            using (var ctx = new TaxiDataDataContext())
            {
                ctx.ObjectTrackingEnabled = false;

                var result = (from r in ctx.CustomerFeedbacks
                              where r.IsApproved == false
                              orderby r.CreateDate descending
                              select new Domain.CustomerFeedback()
                              {
                                  CarRating = r.CarRating,
                                  Comment = r.Comment,
                                  CreateDate = r.CreateDate,
                                  DriverRating = r.DriverRating,
                                  Id = r.CustomerFeedbackId,
                                  IsApproved = r.IsApproved,
                                  PriceRating = r.PriceRating,
                                  ServiceRating = r.ServiceRating,
                                  UserId = r.UserId,
                                  FullName = r.FullName
                              }).ToList();
                return result;
            }
        }

        public bool ApproveReview(long id)
        {
            using (var ctx = new TaxiDataDataContext())
            {
                CustomerFeedback entity;
                try
                {

                    entity = ctx.CustomerFeedbacks.Single(c => c.CustomerFeedbackId == id && !c.IsApproved);
                }
                catch (Exception ex)
                {
                    return false;
                }
                entity.IsApproved = true;

                ctx.SubmitChanges();

                //Update avarage ratings
                var serviceId = entity.TaxiServiceId;
                var service = ctx.TaxiServices.Single(m => m.TaxiServiceId == serviceId);
                service.AvgCarRating = Convert.ToDecimal(ctx.CustomerFeedbacks.Where(m => m.TaxiServiceId == serviceId && m.IsApproved).Average(m => m.CarRating * 1.0));
                service.AvgServiceRating = Convert.ToDecimal(ctx.CustomerFeedbacks.Where(m => m.TaxiServiceId == serviceId && m.IsApproved).Average(m => m.ServiceRating * 1.0));
                service.AvgDriverRating = Convert.ToDecimal(ctx.CustomerFeedbacks.Where(m => m.TaxiServiceId == serviceId && m.IsApproved).Average(m => m.DriverRating * 1.0));
                service.AvgPriceRating = Convert.ToDecimal(ctx.CustomerFeedbacks.Where(m => m.TaxiServiceId == serviceId && m.IsApproved).Average(m => m.PriceRating * 1.0));
                service.AvgRating = (service.AvgPriceRating + service.AvgDriverRating + service.AvgServiceRating + service.AvgCarRating) / 4;
                
                //Saving changes
                ctx.SubmitChanges();
                return true;
            }
        }

    }
}
